#include "sgl_checkbox.h"
#include "stdio.h"

int sgl_checkbox_init(sgl_checkbox_t *checkbox, sgl_obj_t* parent, bool status)
{
    sgl_obj_t* obj;
    if(parent == NULL) {
        return -1;
    }
    checkbox->text = NULL;
    checkbox->font = NULL;
    obj = &checkbox->obj;
    obj->parent = parent;
    obj->event_data = NULL;
    obj->ev_stat = SGL_EVENT_NORMAL;
    obj->obj_type = SGL_OBJ_CHECKBOX;
    sgl_obj_add_src_act_list(obj);
    sgl_obj_add_dirty_list(obj);
    return 0;    
}

sgl_obj_t* sgl_checkbox_create(sgl_obj_t* parent, bool status)
{
    int ret;
    sgl_checkbox_t *checkbox;
    if(parent == NULL) {
        return NULL;
    }
    checkbox = (sgl_checkbox_t *)sgl_alloc(sizeof(sgl_checkbox_t));
    if(checkbox == NULL)
        return NULL;
    ret = sgl_checkbox_init(checkbox, parent, status);
    if(ret) {
        return NULL;
    }
    return  &checkbox->obj;
}

void sgl_checkbox_set_status(sgl_obj_t *obj, bool status)
{
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    if(status != checkbox->status) {
        checkbox->status = status;
        sgl_obj_add_dirty_list(obj);
    }
}

bool sgl_checkbox_get_status(sgl_obj_t *obj)
{
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    return checkbox->status;
}

void sgl_checkbox_click(sgl_obj_t *obj)
{
    sgl_obj_set_event_status(obj, SGL_EVENT_PRESSED);
    sgl_obj_add_dirty_list(obj);
}

void sgl_checkbox_set_font(sgl_obj_t *obj, sgl_font_t *font)
{
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    checkbox->font = font;
}

void sgl_checkbox_set_text(sgl_obj_t *obj, const char* text)
{
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    checkbox->text = text;
}

void sgl_checkbox_draw(sgl_obj_t *obj)
{
    int _start_y = 0;
    int weight = obj->size.h / 6;
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    sgl_widget_draw_rect(rect);
    sgl_widget_buffer_valid(obj);
    sgl_surf_t *surf = sgl_draw_get_active_surf(obj);
    sgl_draw_buffer_clear(surf, obj->parent->style.body_color);
    rect.x2 = rect.x1 + obj->size.h;
    sgl_draw_rect_solid(surf, rect, obj->style.body_color);
    rect.x1 = sgl_widget_draw_ofs_x(weight);
    rect.x2 = sgl_widget_draw_ofs_x(obj->size.h - weight);
    rect.y1 = sgl_widget_draw_ofs_y(weight);
    rect.y2 = sgl_widget_draw_ofs_y(obj->size.h - weight);
    sgl_draw_rect_solid(surf, rect, obj->parent->style.body_color);
    if(checkbox->status) {
        rect.x1 = sgl_widget_draw_ofs_x(weight * 2);
        rect.x2 = sgl_widget_draw_ofs_x(obj->size.h - weight * 2);
        rect.y1 = sgl_widget_draw_ofs_y(weight * 2);
        rect.y2 = sgl_widget_draw_ofs_y(obj->size.h - weight * 2);
        sgl_draw_rect_solid(surf, rect, obj->style.body_color);
    }
    if(checkbox->text && checkbox->font) {
        _start_y = sgl_widget_draw_ofs_y((obj->size.h - sgl_text_height(checkbox->font))/2);
        sgl_draw_font_string(surf, checkbox->text, sgl_widget_draw_ofs_x(obj->size.h + 2), _start_y, obj->style.text_color, obj->parent->style.body_color, checkbox->font); 
    }
    sgl_draw_obj_selected(obj, surf, rect);
    sgl_widget_draw_buffer_flush;
}


void sgl_checkbox_event_cb(sgl_obj_t *obj)
{
    sgl_checkbox_t *checkbox = container_of(obj, sgl_checkbox_t, obj);
    if(obj->ev_stat == SGL_EVENT_PRESSED) {
        checkbox->status = !checkbox->status;
        sgl_obj_event_cb(obj);
    }
    sgl_checkbox_draw(obj);
}
